/*
 * Misc.c
 *
 *  Created on: 2011-7-7
 *      Author: gexiao
 */
/*
 * Miscellaneous utility functions
 */
#include "Dalvik.h"

#include <assert.h>
#include <fcntl.h>

/*
 * Fill out a DebugOutputTarget, suitable for printing to the log.
 */
void dvmCreateLogOutputTarget(DebugOutputTarget* target, int priority,
    const char* tag) {

}

/*
 * Print a debug message, to either a file or the log.
 */
void dvmPrintDebugMessage(const DebugOutputTarget* target, const char* format,
		...) {

}

/*
 * Allocate a bit vector with enough space to hold at least the specified
 * number of bits.
 */
BitVector* dvmAllocBitVector(int startBits, bool expandale) {
	BitVector* bv;
	int count;

	//assert(sizeof(bv->storage[0])==4);
	//assert(startBits>0);

	bv = (BitVector*) malloc(sizeof(BitVector));

	count = (startBits + 31) >> 5;

	bv->storageSize = count;
	bv->expandable = expandale;
	bv->storage = (u4*) malloc(count * sizeof(u4));
	memset(bv->storage, 0xff, count * sizeof(u4));
	return bv;
}

/*
 * Free a BitVector.
 */
void dvmFreeBitVector(BitVector* pBits) {

}

/*
 *
 */
int dvmAllocBit(BitVector* pBits) {
}

/*
 * Mark the specified bit as "clear".
 */
void dvmClearBit(BitVector* pBits, int num) {

}

/*
 *
 */
char* dvmDescriptorToDot(const char* str) {

}

/*
 *
 */

bool dvmIterativeSleep(int iteration, int maxTotalSleep, u8 relStartTime) {

}

/*
 * Set the "close on exec" flag so we don't expose our file descriptors
 * to processes launched by us.
 */bool dvmSetCloseOnExec(int fd) {
	int flags;

	/*
	 * There's presently only one flag defined, so getting the previous
	 * value of the fd flags is probably unnecessary.
	 */
	flags = fcntl(fd, F_GETFD);
	if (flags < 0)
		return false;
	if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
		return false;
	return true;
}

/*
 *  Allocates a memory region using ashmem and mmap, initialized to
 *  zero.  Actual allocation rounded up to page multiple.  Returns
 *  NULL on failure.
 */
void *dvmAllocRegion(size_t size, int prot, const char *name) {

}

/* documented in header file */
const char* dvmPathToAbsolutePortion(const char* path) {
	if (path == NULL) {
		return NULL;
	}

	if (path[0] == '/') {
		/* It's a regular absolute path. Return it. */
		return path;
	}

	const char* sentinel = strstr(path, "/./");

	if (sentinel != NULL) {
		/* It's got the sentinel. Return a pointer to the second slash. */
		return sentinel + 2;
	}

	return NULL;
}
